{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 本周内容：柱状图"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基本柱状图绘制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "    <div class=\"bk-root\">\n",
       "        <a href=\"https://bokeh.org\" target=\"_blank\" class=\"bk-logo bk-logo-small bk-logo-notebook\"></a>\n",
       "        <span id=\"1001\">Loading BokehJS ...</span>\n",
       "    </div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "\n",
       "(function(root) {\n",
       "  function now() {\n",
       "    return new Date();\n",
       "  }\n",
       "\n",
       "  var force = true;\n",
       "\n",
       "  if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n",
       "    root._bokeh_onload_callbacks = [];\n",
       "    root._bokeh_is_loading = undefined;\n",
       "  }\n",
       "\n",
       "  var JS_MIME_TYPE = 'application/javascript';\n",
       "  var HTML_MIME_TYPE = 'text/html';\n",
       "  var EXEC_MIME_TYPE = 'application/vnd.bokehjs_exec.v0+json';\n",
       "  var CLASS_NAME = 'output_bokeh rendered_html';\n",
       "\n",
       "  /**\n",
       "   * Render data to the DOM node\n",
       "   */\n",
       "  function render(props, node) {\n",
       "    var script = document.createElement(\"script\");\n",
       "    node.appendChild(script);\n",
       "  }\n",
       "\n",
       "  /**\n",
       "   * Handle when an output is cleared or removed\n",
       "   */\n",
       "  function handleClearOutput(event, handle) {\n",
       "    var cell = handle.cell;\n",
       "\n",
       "    var id = cell.output_area._bokeh_element_id;\n",
       "    var server_id = cell.output_area._bokeh_server_id;\n",
       "    // Clean up Bokeh references\n",
       "    if (id != null && id in Bokeh.index) {\n",
       "      Bokeh.index[id].model.document.clear();\n",
       "      delete Bokeh.index[id];\n",
       "    }\n",
       "\n",
       "    if (server_id !== undefined) {\n",
       "      // Clean up Bokeh references\n",
       "      var cmd = \"from bokeh.io.state import curstate; print(curstate().uuid_to_server['\" + server_id + \"'].get_sessions()[0].document.roots[0]._id)\";\n",
       "      cell.notebook.kernel.execute(cmd, {\n",
       "        iopub: {\n",
       "          output: function(msg) {\n",
       "            var id = msg.content.text.trim();\n",
       "            if (id in Bokeh.index) {\n",
       "              Bokeh.index[id].model.document.clear();\n",
       "              delete Bokeh.index[id];\n",
       "            }\n",
       "          }\n",
       "        }\n",
       "      });\n",
       "      // Destroy server and session\n",
       "      var cmd = \"import bokeh.io.notebook as ion; ion.destroy_server('\" + server_id + \"')\";\n",
       "      cell.notebook.kernel.execute(cmd);\n",
       "    }\n",
       "  }\n",
       "\n",
       "  /**\n",
       "   * Handle when a new output is added\n",
       "   */\n",
       "  function handleAddOutput(event, handle) {\n",
       "    var output_area = handle.output_area;\n",
       "    var output = handle.output;\n",
       "\n",
       "    // limit handleAddOutput to display_data with EXEC_MIME_TYPE content only\n",
       "    if ((output.output_type != \"display_data\") || (!output.data.hasOwnProperty(EXEC_MIME_TYPE))) {\n",
       "      return\n",
       "    }\n",
       "\n",
       "    var toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n",
       "\n",
       "    if (output.metadata[EXEC_MIME_TYPE][\"id\"] !== undefined) {\n",
       "      toinsert[toinsert.length - 1].firstChild.textContent = output.data[JS_MIME_TYPE];\n",
       "      // store reference to embed id on output_area\n",
       "      output_area._bokeh_element_id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n",
       "    }\n",
       "    if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n",
       "      var bk_div = document.createElement(\"div\");\n",
       "      bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n",
       "      var script_attrs = bk_div.children[0].attributes;\n",
       "      for (var i = 0; i < script_attrs.length; i++) {\n",
       "        toinsert[toinsert.length - 1].firstChild.setAttribute(script_attrs[i].name, script_attrs[i].value);\n",
       "        toinsert[toinsert.length - 1].firstChild.textContent = bk_div.children[0].textContent\n",
       "      }\n",
       "      // store reference to server id on output_area\n",
       "      output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n",
       "    }\n",
       "  }\n",
       "\n",
       "  function register_renderer(events, OutputArea) {\n",
       "\n",
       "    function append_mime(data, metadata, element) {\n",
       "      // create a DOM node to render to\n",
       "      var toinsert = this.create_output_subarea(\n",
       "        metadata,\n",
       "        CLASS_NAME,\n",
       "        EXEC_MIME_TYPE\n",
       "      );\n",
       "      this.keyboard_manager.register_events(toinsert);\n",
       "      // Render to node\n",
       "      var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n",
       "      render(props, toinsert[toinsert.length - 1]);\n",
       "      element.append(toinsert);\n",
       "      return toinsert\n",
       "    }\n",
       "\n",
       "    /* Handle when an output is cleared or removed */\n",
       "    events.on('clear_output.CodeCell', handleClearOutput);\n",
       "    events.on('delete.Cell', handleClearOutput);\n",
       "\n",
       "    /* Handle when a new output is added */\n",
       "    events.on('output_added.OutputArea', handleAddOutput);\n",
       "\n",
       "    /**\n",
       "     * Register the mime type and append_mime function with output_area\n",
       "     */\n",
       "    OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n",
       "      /* Is output safe? */\n",
       "      safe: true,\n",
       "      /* Index of renderer in `output_area.display_order` */\n",
       "      index: 0\n",
       "    });\n",
       "  }\n",
       "\n",
       "  // register the mime type if in Jupyter Notebook environment and previously unregistered\n",
       "  if (root.Jupyter !== undefined) {\n",
       "    var events = require('base/js/events');\n",
       "    var OutputArea = require('notebook/js/outputarea').OutputArea;\n",
       "\n",
       "    if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n",
       "      register_renderer(events, OutputArea);\n",
       "    }\n",
       "  }\n",
       "\n",
       "  \n",
       "  if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n",
       "    root._bokeh_timeout = Date.now() + 5000;\n",
       "    root._bokeh_failed_load = false;\n",
       "  }\n",
       "\n",
       "  var NB_LOAD_WARNING = {'data': {'text/html':\n",
       "     \"<div style='background-color: #fdd'>\\n\"+\n",
       "     \"<p>\\n\"+\n",
       "     \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n",
       "     \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n",
       "     \"</p>\\n\"+\n",
       "     \"<ul>\\n\"+\n",
       "     \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n",
       "     \"<li>use INLINE resources instead, as so:</li>\\n\"+\n",
       "     \"</ul>\\n\"+\n",
       "     \"<code>\\n\"+\n",
       "     \"from bokeh.resources import INLINE\\n\"+\n",
       "     \"output_notebook(resources=INLINE)\\n\"+\n",
       "     \"</code>\\n\"+\n",
       "     \"</div>\"}};\n",
       "\n",
       "  function display_loaded() {\n",
       "    var el = document.getElementById(\"1001\");\n",
       "    if (el != null) {\n",
       "      el.textContent = \"BokehJS is loading...\";\n",
       "    }\n",
       "    if (root.Bokeh !== undefined) {\n",
       "      if (el != null) {\n",
       "        el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n",
       "      }\n",
       "    } else if (Date.now() < root._bokeh_timeout) {\n",
       "      setTimeout(display_loaded, 100)\n",
       "    }\n",
       "  }\n",
       "\n",
       "\n",
       "  function run_callbacks() {\n",
       "    try {\n",
       "      root._bokeh_onload_callbacks.forEach(function(callback) {\n",
       "        if (callback != null)\n",
       "          callback();\n",
       "      });\n",
       "    } finally {\n",
       "      delete root._bokeh_onload_callbacks\n",
       "    }\n",
       "    console.debug(\"Bokeh: all callbacks have finished\");\n",
       "  }\n",
       "\n",
       "  function load_libs(css_urls, js_urls, callback) {\n",
       "    if (css_urls == null) css_urls = [];\n",
       "    if (js_urls == null) js_urls = [];\n",
       "\n",
       "    root._bokeh_onload_callbacks.push(callback);\n",
       "    if (root._bokeh_is_loading > 0) {\n",
       "      console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n",
       "      return null;\n",
       "    }\n",
       "    if (js_urls == null || js_urls.length === 0) {\n",
       "      run_callbacks();\n",
       "      return null;\n",
       "    }\n",
       "    console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
       "    root._bokeh_is_loading = css_urls.length + js_urls.length;\n",
       "\n",
       "    function on_load() {\n",
       "      root._bokeh_is_loading--;\n",
       "      if (root._bokeh_is_loading === 0) {\n",
       "        console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n",
       "        run_callbacks()\n",
       "      }\n",
       "    }\n",
       "\n",
       "    function on_error() {\n",
       "      console.error(\"failed to load \" + url);\n",
       "    }\n",
       "\n",
       "    for (var i = 0; i < css_urls.length; i++) {\n",
       "      var url = css_urls[i];\n",
       "      const element = document.createElement(\"link\");\n",
       "      element.onload = on_load;\n",
       "      element.onerror = on_error;\n",
       "      element.rel = \"stylesheet\";\n",
       "      element.type = \"text/css\";\n",
       "      element.href = url;\n",
       "      console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n",
       "      document.body.appendChild(element);\n",
       "    }\n",
       "\n",
       "    const hashes = {\"https://cdn.bokeh.org/bokeh/release/bokeh-2.1.1.min.js\": \"kLr4fYcqcSpbuI95brIH3vnnYCquzzSxHPU6XGQCIkQRGJwhg0StNbj1eegrHs12\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.1.1.min.js\": \"xIGPmVtaOm+z0BqfSOMn4lOR6ciex448GIKG4eE61LsAvmGj48XcMQZtKcE/UXZe\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.1.1.min.js\": \"Dc9u1wF/0zApGIWoBbH77iWEHtdmkuYWG839Uzmv8y8yBLXebjO9ZnERsde5Ln/P\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-2.1.1.min.js\": \"cT9JaBz7GiRXdENrJLZNSC6eMNF3nh3fa5fTF51Svp+ukxPdwcU5kGXGPBgDCa2j\"};\n",
       "\n",
       "    for (var i = 0; i < js_urls.length; i++) {\n",
       "      var url = js_urls[i];\n",
       "      var element = document.createElement('script');\n",
       "      element.onload = on_load;\n",
       "      element.onerror = on_error;\n",
       "      element.async = false;\n",
       "      element.src = url;\n",
       "      if (url in hashes) {\n",
       "        element.crossOrigin = \"anonymous\";\n",
       "        element.integrity = \"sha384-\" + hashes[url];\n",
       "      }\n",
       "      console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
       "      document.head.appendChild(element);\n",
       "    }\n",
       "  };\n",
       "\n",
       "  function inject_raw_css(css) {\n",
       "    const element = document.createElement(\"style\");\n",
       "    element.appendChild(document.createTextNode(css));\n",
       "    document.body.appendChild(element);\n",
       "  }\n",
       "\n",
       "  \n",
       "  var js_urls = [\"https://cdn.bokeh.org/bokeh/release/bokeh-2.1.1.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.1.1.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.1.1.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-2.1.1.min.js\"];\n",
       "  var css_urls = [];\n",
       "  \n",
       "\n",
       "  var inline_js = [\n",
       "    function(Bokeh) {\n",
       "      Bokeh.set_log_level(\"info\");\n",
       "    },\n",
       "    function(Bokeh) {\n",
       "    \n",
       "    \n",
       "    }\n",
       "  ];\n",
       "\n",
       "  function run_inline_js() {\n",
       "    \n",
       "    if (root.Bokeh !== undefined || force === true) {\n",
       "      \n",
       "    for (var i = 0; i < inline_js.length; i++) {\n",
       "      inline_js[i].call(root, root.Bokeh);\n",
       "    }\n",
       "    if (force === true) {\n",
       "        display_loaded();\n",
       "      }} else if (Date.now() < root._bokeh_timeout) {\n",
       "      setTimeout(run_inline_js, 100);\n",
       "    } else if (!root._bokeh_failed_load) {\n",
       "      console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n",
       "      root._bokeh_failed_load = true;\n",
       "    } else if (force !== true) {\n",
       "      var cell = $(document.getElementById(\"1001\")).parents('.cell').data().cell;\n",
       "      cell.output_area.append_execute_result(NB_LOAD_WARNING)\n",
       "    }\n",
       "\n",
       "  }\n",
       "\n",
       "  if (root._bokeh_is_loading === 0) {\n",
       "    console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
       "    run_inline_js();\n",
       "  } else {\n",
       "    load_libs(css_urls, js_urls, function() {\n",
       "      console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n",
       "      run_inline_js();\n",
       "    });\n",
       "  }\n",
       "}(window));"
      ],
      "application/vnd.bokehjs_load.v0+json": "\n(function(root) {\n  function now() {\n    return new Date();\n  }\n\n  var force = true;\n\n  if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n    root._bokeh_onload_callbacks = [];\n    root._bokeh_is_loading = undefined;\n  }\n\n  \n\n  \n  if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n    root._bokeh_timeout = Date.now() + 5000;\n    root._bokeh_failed_load = false;\n  }\n\n  var NB_LOAD_WARNING = {'data': {'text/html':\n     \"<div style='background-color: #fdd'>\\n\"+\n     \"<p>\\n\"+\n     \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n     \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n     \"</p>\\n\"+\n     \"<ul>\\n\"+\n     \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n     \"<li>use INLINE resources instead, as so:</li>\\n\"+\n     \"</ul>\\n\"+\n     \"<code>\\n\"+\n     \"from bokeh.resources import INLINE\\n\"+\n     \"output_notebook(resources=INLINE)\\n\"+\n     \"</code>\\n\"+\n     \"</div>\"}};\n\n  function display_loaded() {\n    var el = document.getElementById(\"1001\");\n    if (el != null) {\n      el.textContent = \"BokehJS is loading...\";\n    }\n    if (root.Bokeh !== undefined) {\n      if (el != null) {\n        el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n      }\n    } else if (Date.now() < root._bokeh_timeout) {\n      setTimeout(display_loaded, 100)\n    }\n  }\n\n\n  function run_callbacks() {\n    try {\n      root._bokeh_onload_callbacks.forEach(function(callback) {\n        if (callback != null)\n          callback();\n      });\n    } finally {\n      delete root._bokeh_onload_callbacks\n    }\n    console.debug(\"Bokeh: all callbacks have finished\");\n  }\n\n  function load_libs(css_urls, js_urls, callback) {\n    if (css_urls == null) css_urls = [];\n    if (js_urls == null) js_urls = [];\n\n    root._bokeh_onload_callbacks.push(callback);\n    if (root._bokeh_is_loading > 0) {\n      console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n      return null;\n    }\n    if (js_urls == null || js_urls.length === 0) {\n      run_callbacks();\n      return null;\n    }\n    console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n    root._bokeh_is_loading = css_urls.length + js_urls.length;\n\n    function on_load() {\n      root._bokeh_is_loading--;\n      if (root._bokeh_is_loading === 0) {\n        console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n        run_callbacks()\n      }\n    }\n\n    function on_error() {\n      console.error(\"failed to load \" + url);\n    }\n\n    for (var i = 0; i < css_urls.length; i++) {\n      var url = css_urls[i];\n      const element = document.createElement(\"link\");\n      element.onload = on_load;\n      element.onerror = on_error;\n      element.rel = \"stylesheet\";\n      element.type = \"text/css\";\n      element.href = url;\n      console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n      document.body.appendChild(element);\n    }\n\n    const hashes = {\"https://cdn.bokeh.org/bokeh/release/bokeh-2.1.1.min.js\": \"kLr4fYcqcSpbuI95brIH3vnnYCquzzSxHPU6XGQCIkQRGJwhg0StNbj1eegrHs12\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.1.1.min.js\": \"xIGPmVtaOm+z0BqfSOMn4lOR6ciex448GIKG4eE61LsAvmGj48XcMQZtKcE/UXZe\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.1.1.min.js\": \"Dc9u1wF/0zApGIWoBbH77iWEHtdmkuYWG839Uzmv8y8yBLXebjO9ZnERsde5Ln/P\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-2.1.1.min.js\": \"cT9JaBz7GiRXdENrJLZNSC6eMNF3nh3fa5fTF51Svp+ukxPdwcU5kGXGPBgDCa2j\"};\n\n    for (var i = 0; i < js_urls.length; i++) {\n      var url = js_urls[i];\n      var element = document.createElement('script');\n      element.onload = on_load;\n      element.onerror = on_error;\n      element.async = false;\n      element.src = url;\n      if (url in hashes) {\n        element.crossOrigin = \"anonymous\";\n        element.integrity = \"sha384-\" + hashes[url];\n      }\n      console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n      document.head.appendChild(element);\n    }\n  };\n\n  function inject_raw_css(css) {\n    const element = document.createElement(\"style\");\n    element.appendChild(document.createTextNode(css));\n    document.body.appendChild(element);\n  }\n\n  \n  var js_urls = [\"https://cdn.bokeh.org/bokeh/release/bokeh-2.1.1.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.1.1.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.1.1.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-2.1.1.min.js\"];\n  var css_urls = [];\n  \n\n  var inline_js = [\n    function(Bokeh) {\n      Bokeh.set_log_level(\"info\");\n    },\n    function(Bokeh) {\n    \n    \n    }\n  ];\n\n  function run_inline_js() {\n    \n    if (root.Bokeh !== undefined || force === true) {\n      \n    for (var i = 0; i < inline_js.length; i++) {\n      inline_js[i].call(root, root.Bokeh);\n    }\n    if (force === true) {\n        display_loaded();\n      }} else if (Date.now() < root._bokeh_timeout) {\n      setTimeout(run_inline_js, 100);\n    } else if (!root._bokeh_failed_load) {\n      console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n      root._bokeh_failed_load = true;\n    } else if (force !== true) {\n      var cell = $(document.getElementById(\"1001\")).parents('.cell').data().cell;\n      cell.output_area.append_execute_result(NB_LOAD_WARNING)\n    }\n\n  }\n\n  if (root._bokeh_is_loading === 0) {\n    console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n    run_inline_js();\n  } else {\n    load_libs(css_urls, js_urls, function() {\n      console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n      run_inline_js();\n    });\n  }\n}(window));"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from bokeh.plotting import output_notebook,figure,show\n",
    "output_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 垂直柱状图\n",
    "\n",
    "* 绘制方法\n",
    "> vbar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'p' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-0e7492133b28>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mhelp\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvbar\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'p' is not defined"
     ]
    }
   ],
   "source": [
    "help(p.vbar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 画布\n",
    "p = figure(plot_width=400,plot_height=400)\n",
    "\n",
    "p.vbar(\n",
    "    x=[1,2,3],\n",
    "    width=0.5,\n",
    "    bottom=0,\n",
    "    top=[1.2,2.5,3.7],\n",
    "    color=\"red\"\n",
    ")\n",
    "\n",
    "#显示\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 水平柱状图\n",
    "* 绘制方法\n",
    "> hbar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "?? p.hbar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 画布\n",
    "p=figure(plot_width=400,plot_height=400,title=\"水平柱状图Demo\")\n",
    "\n",
    "# 绘制\n",
    "p.hbar(\n",
    "    y=[1,2,3],\n",
    "    height=0.5,\n",
    "    left=0,\n",
    "    right=[1.2,2.5,3.7],\n",
    "    color=\"navy\"\n",
    ")\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 案例DOME（1）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"2bfd6b38-bd55-4b41-873d-fb0a4a8552f0\" data-root-id=\"1003\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  var docs_json = {\"044d519c-71c7-4005-94cc-c7f2a1fd7ea2\":{\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1014\"}],\"center\":[{\"id\":\"1016\"},{\"id\":\"1020\"}],\"left\":[{\"id\":\"1017\"}],\"plot_height\":350,\"renderers\":[{\"id\":\"1040\"}],\"title\":{\"id\":\"1004\"},\"toolbar\":{\"id\":\"1029\"},\"x_range\":{\"id\":\"1006\"},\"x_scale\":{\"id\":\"1010\"},\"y_range\":{\"id\":\"1008\"},\"y_scale\":{\"id\":\"1012\"}},\"id\":\"1003\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"formatter\":{\"id\":\"1044\"},\"ticker\":{\"id\":\"1015\"}},\"id\":\"1014\",\"type\":\"CategoricalAxis\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.8},\"x\":{\"field\":\"x\"}},\"id\":\"1038\",\"type\":\"VBar\"},{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"1027\",\"type\":\"BoxAnnotation\"},{\"attributes\":{},\"id\":\"1010\",\"type\":\"CategoricalScale\"},{\"attributes\":{},\"id\":\"1046\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"1012\",\"type\":\"LinearScale\"},{\"attributes\":{\"factors\":[\"Apples\",\"Pears\",\"Nectarines\",\"Plums\",\"Grapes\",\"Strawberries\"]},\"id\":\"1006\",\"type\":\"FactorRange\"},{\"attributes\":{\"start\":0},\"id\":\"1008\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"1015\",\"type\":\"CategoricalTicker\"},{\"attributes\":{\"axis\":{\"id\":\"1014\"},\"grid_line_color\":null,\"ticker\":null},\"id\":\"1016\",\"type\":\"Grid\"},{\"attributes\":{\"formatter\":{\"id\":\"1046\"},\"ticker\":{\"id\":\"1018\"}},\"id\":\"1017\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1018\",\"type\":\"BasicTicker\"},{\"attributes\":{\"axis\":{\"id\":\"1017\"},\"dimension\":1,\"ticker\":null},\"id\":\"1020\",\"type\":\"Grid\"},{\"attributes\":{\"overlay\":{\"id\":\"1027\"}},\"id\":\"1023\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"1026\",\"type\":\"HelpTool\"},{\"attributes\":{\"callback\":null,\"tooltips\":[[\"count\",\"@counts\"]]},\"id\":\"1028\",\"type\":\"HoverTool\"},{\"attributes\":{},\"id\":\"1021\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"1022\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"data_source\":{\"id\":\"1002\"},\"glyph\":{\"id\":\"1038\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"1039\"},\"selection_glyph\":null,\"view\":{\"id\":\"1041\"}},\"id\":\"1040\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.8},\"x\":{\"field\":\"x\"}},\"id\":\"1039\",\"type\":\"VBar\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"1021\"},{\"id\":\"1022\"},{\"id\":\"1023\"},{\"id\":\"1024\"},{\"id\":\"1025\"},{\"id\":\"1026\"},{\"id\":\"1028\"}]},\"id\":\"1029\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"1024\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"1025\",\"type\":\"ResetTool\"},{\"attributes\":{},\"id\":\"1048\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1049\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"1044\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{\"source\":{\"id\":\"1002\"}},\"id\":\"1041\",\"type\":\"CDSView\"},{\"attributes\":{\"data\":{\"counts\":[5,3,4,2,4,6],\"x\":[\"Apples\",\"Pears\",\"Nectarines\",\"Plums\",\"Grapes\",\"Strawberries\"]},\"selected\":{\"id\":\"1048\"},\"selection_policy\":{\"id\":\"1049\"}},\"id\":\"1002\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"text\":\"Fruits Counts\"},\"id\":\"1004\",\"type\":\"Title\"}],\"root_ids\":[\"1003\"]},\"title\":\"Bokeh Application\",\"version\":\"2.1.1\"}};\n",
       "  var render_items = [{\"docid\":\"044d519c-71c7-4005-94cc-c7f2a1fd7ea2\",\"root_ids\":[\"1003\"],\"roots\":{\"1003\":\"2bfd6b38-bd55-4b41-873d-fb0a4a8552f0\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    var attempts = 0;\n",
       "    var timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "1003"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from bokeh.models import ColumnDataSource\n",
    "\n",
    "# 数据准备（名称和值）\n",
    "fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']\n",
    "counts = [5, 3, 4, 2, 4, 6]\n",
    "\n",
    "# 数据源准备\n",
    "source = ColumnDataSource(\n",
    "    data = dict(\n",
    "        x = fruits,\n",
    "        counts = counts\n",
    "    )\n",
    ") # 尝试鼠标移入显示数值\n",
    "TOOLTIPS = [\n",
    "    (\"count\",\"@counts\")\n",
    "]\n",
    "\n",
    "# 画布\n",
    "p = figure(\n",
    "    x_range=fruits,\n",
    "    plot_height=350,\n",
    "    title=\"Fruits Counts\",\n",
    "    tooltips=TOOLTIPS\n",
    ")\n",
    "\n",
    "# 绘制\n",
    "# p.vbar(x=fruits,top=counts,width=0.5)\n",
    "p.vbar(x='x',top='counts',width=0.8,source=source)\n",
    "\n",
    "p.xgrid.grid_line_color = None\n",
    "p.y_range.start=0 #条形最低端与x轴的距离\n",
    "# p.legend.location = \"left\"\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using data directory: C:\\Users\\lenovo\\.bokeh\\data\n",
      "Skipping 'CGM.csv' (checksum match)\n",
      "Skipping 'US_Counties.zip' (checksum match)\n",
      "Skipping 'us_cities.json' (checksum match)\n",
      "Skipping 'unemployment09.csv' (checksum match)\n",
      "Skipping 'AAPL.csv' (checksum match)\n",
      "Skipping 'FB.csv' (checksum match)\n",
      "Skipping 'GOOG.csv' (checksum match)\n",
      "Skipping 'IBM.csv' (checksum match)\n",
      "Skipping 'MSFT.csv' (checksum match)\n",
      "Skipping 'WPP2012_SA_DB03_POPULATION_QUINQUENNIAL.zip' (checksum match)\n",
      "Skipping 'gapminder_fertility.csv' (checksum match)\n",
      "Skipping 'gapminder_population.csv' (checksum match)\n",
      "Skipping 'gapminder_life_expectancy.csv' (checksum match)\n",
      "Skipping 'gapminder_regions.csv' (checksum match)\n",
      "Skipping 'world_cities.zip' (checksum match)\n",
      "Skipping 'airports.json' (checksum match)\n",
      "Skipping 'movies.db.zip' (checksum match)\n",
      "Skipping 'airports.csv' (checksum match)\n",
      "Skipping 'routes.csv' (checksum match)\n",
      "Skipping 'haarcascade_frontalface_default.xml' (checksum match)\n"
     ]
    }
   ],
   "source": [
    "import bokeh\n",
    "bokeh.sampledata.download()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 本人实践：基本柱状图\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Visualization_tools</th>\n",
       "      <th>Watch</th>\n",
       "      <th>Star</th>\n",
       "      <th>Fork</th>\n",
       "      <th>Commits</th>\n",
       "      <th>Contributors</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>matplotlib</td>\n",
       "      <td>533</td>\n",
       "      <td>9678</td>\n",
       "      <td>4143</td>\n",
       "      <td>29503</td>\n",
       "      <td>808</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bokeh</td>\n",
       "      <td>396</td>\n",
       "      <td>11034</td>\n",
       "      <td>2526</td>\n",
       "      <td>17673</td>\n",
       "      <td>357</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>pyecharts</td>\n",
       "      <td>263</td>\n",
       "      <td>5387</td>\n",
       "      <td>1148</td>\n",
       "      <td>1321</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>seaborn</td>\n",
       "      <td>234</td>\n",
       "      <td>6038</td>\n",
       "      <td>970</td>\n",
       "      <td>2316</td>\n",
       "      <td>98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>plotly</td>\n",
       "      <td>234</td>\n",
       "      <td>4928</td>\n",
       "      <td>1114</td>\n",
       "      <td>3370</td>\n",
       "      <td>76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>ggplot2</td>\n",
       "      <td>329</td>\n",
       "      <td>3783</td>\n",
       "      <td>1427</td>\n",
       "      <td>4286</td>\n",
       "      <td>184</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Visualization_tools  Watch   Star  Fork  Commits  Contributors\n",
       "0          matplotlib    533   9678  4143    29503           808\n",
       "1               bokeh    396  11034  2526    17673           357\n",
       "2           pyecharts    263   5387  1148     1321            18\n",
       "3             seaborn    234   6038   970     2316            98\n",
       "4              plotly    234   4928  1114     3370            76\n",
       "5             ggplot2    329   3783  1427     4286           184"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据源准备\n",
    "import pandas as pd\n",
    "df=pd.read_csv('C:/Users/lenovo/.bokeh/data/visualization-20190505.csv')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     9678\n",
       "1    11034\n",
       "2     5387\n",
       "3     6038\n",
       "4     4928\n",
       "5     3783\n",
       "Name: Star, dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['Star']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    matplotlib\n",
       "1         bokeh\n",
       "2     pyecharts\n",
       "3       seaborn\n",
       "4        plotly\n",
       "5       ggplot2\n",
       "Name: Visualization_tools, dtype: object"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['Visualization_tools']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Star\n",
    "# Visualization_tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"2bf00012-e395-4501-a95e-5734fd4ba9d6\" data-root-id=\"1099\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  var docs_json = {\"c133c1f0-dc1b-4d85-99e6-32694d1b7f7f\":{\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1110\"}],\"center\":[{\"id\":\"1112\"},{\"id\":\"1116\"}],\"left\":[{\"id\":\"1113\"}],\"plot_height\":350,\"renderers\":[{\"id\":\"1136\"}],\"title\":{\"id\":\"1100\"},\"toolbar\":{\"id\":\"1125\"},\"x_range\":{\"id\":\"1102\"},\"x_scale\":{\"id\":\"1106\"},\"y_range\":{\"id\":\"1104\"},\"y_scale\":{\"id\":\"1108\"}},\"id\":\"1099\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"text\":\"yy\"},\"id\":\"1100\",\"type\":\"Title\"},{\"attributes\":{\"data\":{\"star\":[9678,11034,5387,6038,4928,3783],\"x\":[\"matplotlib\",\"bokeh\",\"pyecharts\",\"seaborn\",\"plotly\",\"ggplot2\"]},\"selected\":{\"id\":\"1152\"},\"selection_policy\":{\"id\":\"1153\"}},\"id\":\"1098\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"1108\",\"type\":\"LinearScale\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"star\"},\"width\":{\"value\":0.8},\"x\":{\"field\":\"x\"}},\"id\":\"1134\",\"type\":\"VBar\"},{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"1123\",\"type\":\"BoxAnnotation\"},{\"attributes\":{},\"id\":\"1106\",\"type\":\"CategoricalScale\"},{\"attributes\":{\"formatter\":{\"id\":\"1148\"},\"ticker\":{\"id\":\"1111\"}},\"id\":\"1110\",\"type\":\"CategoricalAxis\"},{\"attributes\":{\"factors\":[\"matplotlib\",\"bokeh\",\"pyecharts\",\"seaborn\",\"plotly\",\"ggplot2\"]},\"id\":\"1102\",\"type\":\"FactorRange\"},{\"attributes\":{},\"id\":\"1148\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{},\"id\":\"1117\",\"type\":\"PanTool\"},{\"attributes\":{\"start\":0},\"id\":\"1104\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"1111\",\"type\":\"CategoricalTicker\"},{\"attributes\":{\"axis\":{\"id\":\"1110\"},\"grid_line_color\":null,\"ticker\":null},\"id\":\"1112\",\"type\":\"Grid\"},{\"attributes\":{\"formatter\":{\"id\":\"1150\"},\"ticker\":{\"id\":\"1114\"}},\"id\":\"1113\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1114\",\"type\":\"BasicTicker\"},{\"attributes\":{\"axis\":{\"id\":\"1113\"},\"dimension\":1,\"ticker\":null},\"id\":\"1116\",\"type\":\"Grid\"},{\"attributes\":{\"callback\":null,\"tooltips\":[[\"star\",\"@star\"]]},\"id\":\"1124\",\"type\":\"HoverTool\"},{\"attributes\":{},\"id\":\"1150\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"1118\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"star\"},\"width\":{\"value\":0.8},\"x\":{\"field\":\"x\"}},\"id\":\"1135\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"1122\",\"type\":\"HelpTool\"},{\"attributes\":{\"overlay\":{\"id\":\"1123\"}},\"id\":\"1119\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"1120\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"1152\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1121\",\"type\":\"ResetTool\"},{\"attributes\":{\"data_source\":{\"id\":\"1098\"},\"glyph\":{\"id\":\"1134\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"1135\"},\"selection_glyph\":null,\"view\":{\"id\":\"1137\"}},\"id\":\"1136\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"1153\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"1117\"},{\"id\":\"1118\"},{\"id\":\"1119\"},{\"id\":\"1120\"},{\"id\":\"1121\"},{\"id\":\"1122\"},{\"id\":\"1124\"}]},\"id\":\"1125\",\"type\":\"Toolbar\"},{\"attributes\":{\"source\":{\"id\":\"1098\"}},\"id\":\"1137\",\"type\":\"CDSView\"}],\"root_ids\":[\"1099\"]},\"title\":\"Bokeh Application\",\"version\":\"2.1.1\"}};\n",
       "  var render_items = [{\"docid\":\"c133c1f0-dc1b-4d85-99e6-32694d1b7f7f\",\"root_ids\":[\"1099\"],\"roots\":{\"1099\":\"2bf00012-e395-4501-a95e-5734fd4ba9d6\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    var attempts = 0;\n",
       "    var timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "1099"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "Visualization_tools = df['Visualization_tools']\n",
    "Star = df['Star']\n",
    "# 数据源准备\n",
    "source = ColumnDataSource(\n",
    "    data = dict(\n",
    "        x = Visualization_tools,\n",
    "        star = Star\n",
    "    )\n",
    ") # 尝试鼠标移入显示数值\n",
    "TOOLTIPS = [\n",
    "    (\"star\",\"@star\")\n",
    "]\n",
    "\n",
    "# 画布\n",
    "p = figure(\n",
    "    x_range=Visualization_tools,\n",
    "    plot_height=350,\n",
    "    title=\"yy\",\n",
    "    tooltips=TOOLTIPS\n",
    ")\n",
    "\n",
    "# 绘制\n",
    "# p.vbar(x=fruits,top=counts,width=0.5)\n",
    "p.vbar(x='x',top='star',width=0.8,source=source)\n",
    "\n",
    "p.xgrid.grid_line_color = None\n",
    "p.y_range.start=0 #条形最低端与x轴的距离\n",
    "# p.legend.location = \"left\"\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"09ebab8b-f360-465f-a2f8-9027899e32fa\" data-root-id=\"1203\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  var docs_json = {\"285d8690-3cac-49ac-92cf-a7f977984665\":{\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1214\"}],\"center\":[{\"id\":\"1216\"},{\"id\":\"1220\"}],\"left\":[{\"id\":\"1217\"}],\"plot_height\":350,\"renderers\":[{\"id\":\"1240\"}],\"title\":{\"id\":\"1204\"},\"toolbar\":{\"id\":\"1229\"},\"x_range\":{\"id\":\"1206\"},\"x_scale\":{\"id\":\"1210\"},\"y_range\":{\"id\":\"1208\"},\"y_scale\":{\"id\":\"1212\"}},\"id\":\"1203\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"source\":{\"id\":\"1202\"}},\"id\":\"1241\",\"type\":\"CDSView\"},{\"attributes\":{\"text\":\"ll\"},\"id\":\"1204\",\"type\":\"Title\"},{\"attributes\":{\"data\":{\"commits\":[29503,17673,1321,2316,3370,4286],\"x\":[\"matplotlib\",\"bokeh\",\"pyecharts\",\"seaborn\",\"plotly\",\"ggplot2\"]},\"selected\":{\"id\":\"1264\"},\"selection_policy\":{\"id\":\"1265\"}},\"id\":\"1202\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"1212\",\"type\":\"LinearScale\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"commits\"},\"width\":{\"value\":0.8},\"x\":{\"field\":\"x\"}},\"id\":\"1238\",\"type\":\"VBar\"},{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"1227\",\"type\":\"BoxAnnotation\"},{\"attributes\":{},\"id\":\"1218\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"1262\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"1210\",\"type\":\"CategoricalScale\"},{\"attributes\":{},\"id\":\"1222\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"1260\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{\"formatter\":{\"id\":\"1260\"},\"ticker\":{\"id\":\"1215\"}},\"id\":\"1214\",\"type\":\"CategoricalAxis\"},{\"attributes\":{\"factors\":[\"matplotlib\",\"bokeh\",\"pyecharts\",\"seaborn\",\"plotly\",\"ggplot2\"]},\"id\":\"1206\",\"type\":\"FactorRange\"},{\"attributes\":{},\"id\":\"1264\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1265\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"1221\",\"type\":\"PanTool\"},{\"attributes\":{\"start\":0},\"id\":\"1208\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"1215\",\"type\":\"CategoricalTicker\"},{\"attributes\":{\"axis\":{\"id\":\"1214\"},\"grid_line_color\":null,\"ticker\":null},\"id\":\"1216\",\"type\":\"Grid\"},{\"attributes\":{\"formatter\":{\"id\":\"1262\"},\"ticker\":{\"id\":\"1218\"}},\"id\":\"1217\",\"type\":\"LinearAxis\"},{\"attributes\":{\"axis\":{\"id\":\"1217\"},\"dimension\":1,\"ticker\":null},\"id\":\"1220\",\"type\":\"Grid\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"commits\"},\"width\":{\"value\":0.8},\"x\":{\"field\":\"x\"}},\"id\":\"1239\",\"type\":\"VBar\"},{\"attributes\":{\"callback\":null,\"tooltips\":[[\"commits\",\"@commits\"]]},\"id\":\"1228\",\"type\":\"HoverTool\"},{\"attributes\":{},\"id\":\"1226\",\"type\":\"HelpTool\"},{\"attributes\":{\"data_source\":{\"id\":\"1202\"},\"glyph\":{\"id\":\"1238\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"1239\"},\"selection_glyph\":null,\"view\":{\"id\":\"1241\"}},\"id\":\"1240\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"overlay\":{\"id\":\"1227\"}},\"id\":\"1223\",\"type\":\"BoxZoomTool\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"1221\"},{\"id\":\"1222\"},{\"id\":\"1223\"},{\"id\":\"1224\"},{\"id\":\"1225\"},{\"id\":\"1226\"},{\"id\":\"1228\"}]},\"id\":\"1229\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"1224\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"1225\",\"type\":\"ResetTool\"}],\"root_ids\":[\"1203\"]},\"title\":\"Bokeh Application\",\"version\":\"2.1.1\"}};\n",
       "  var render_items = [{\"docid\":\"285d8690-3cac-49ac-92cf-a7f977984665\",\"root_ids\":[\"1203\"],\"roots\":{\"1203\":\"09ebab8b-f360-465f-a2f8-9027899e32fa\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    var attempts = 0;\n",
       "    var timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "1203"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "Visualization_tools = df['Visualization_tools']\n",
    "Commits = df['Commits']\n",
    "# 数据源准备\n",
    "source = ColumnDataSource(\n",
    "    data = dict(\n",
    "        x = Visualization_tools,\n",
    "        commits = Commits\n",
    "    )\n",
    ") # 尝试鼠标移入显示数值\n",
    "TOOLTIPS = [\n",
    "    (\"commits\",\"@commits\")\n",
    "]\n",
    "\n",
    "# 画布\n",
    "p = figure(\n",
    "    x_range=Visualization_tools,\n",
    "    plot_height=350,\n",
    "    title=\"ll\",\n",
    "    tooltips=TOOLTIPS\n",
    ")\n",
    "\n",
    "# 绘制\n",
    "# p.vbar(x=fruits,top=counts,width=0.5)\n",
    "p.vbar(x='x',top='commits',width=0.8,source=source)\n",
    "\n",
    "p.xgrid.grid_line_color = None\n",
    "p.y_range.start=0 #条形最低端与x轴的距离\n",
    "# p.legend.location = \"left\"\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 老师代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "BokehDeprecationWarning: 'legend' keyword is deprecated, use explicit 'legend_label', 'legend_field', or 'legend_group' keywords instead\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"0e32c74d-820a-40f9-8c41-450c5b2aea07\" data-root-id=\"1315\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  var docs_json = {\"70559d29-81cc-4473-adb4-890da6cb7395\":{\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1326\"}],\"center\":[{\"id\":\"1328\"},{\"id\":\"1332\"},{\"id\":\"1362\"}],\"left\":[{\"id\":\"1329\"}],\"plot_height\":500,\"renderers\":[{\"id\":\"1352\"}],\"title\":{\"id\":\"1316\"},\"toolbar\":{\"id\":\"1341\"},\"x_range\":{\"id\":\"1318\"},\"x_scale\":{\"id\":\"1322\"},\"y_range\":{\"id\":\"1320\"},\"y_scale\":{\"id\":\"1324\"}},\"id\":\"1315\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{},\"id\":\"1356\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{\"label\":{\"field\":\"tools\"},\"renderers\":[{\"id\":\"1352\"}]},\"id\":\"1363\",\"type\":\"LegendItem\"},{\"attributes\":{},\"id\":\"1338\",\"type\":\"HelpTool\"},{\"attributes\":{\"callback\":null,\"tooltips\":[[\"star\",\"@star_counts\"],[\"tools\",\"@tools\"]]},\"id\":\"1340\",\"type\":\"HoverTool\"},{\"attributes\":{},\"id\":\"1334\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"1336\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"1324\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"1358\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"1339\",\"type\":\"BoxAnnotation\"},{\"attributes\":{},\"id\":\"1333\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"1322\",\"type\":\"CategoricalScale\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"field\":\"color\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"field\":\"color\"},\"top\":{\"field\":\"star_counts\"},\"width\":{\"value\":0.8},\"x\":{\"field\":\"tools\"}},\"id\":\"1351\",\"type\":\"VBar\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"1333\"},{\"id\":\"1334\"},{\"id\":\"1335\"},{\"id\":\"1336\"},{\"id\":\"1337\"},{\"id\":\"1338\"},{\"id\":\"1340\"}]},\"id\":\"1341\",\"type\":\"Toolbar\"},{\"attributes\":{\"formatter\":{\"id\":\"1358\"},\"ticker\":{\"id\":\"1330\"}},\"id\":\"1329\",\"type\":\"LinearAxis\"},{\"attributes\":{\"source\":{\"id\":\"1314\"}},\"id\":\"1353\",\"type\":\"CDSView\"},{\"attributes\":{\"items\":[{\"id\":\"1363\"}],\"location\":\"top_center\",\"orientation\":\"horizontal\"},\"id\":\"1362\",\"type\":\"Legend\"},{\"attributes\":{\"fill_color\":{\"field\":\"color\"},\"line_color\":{\"field\":\"color\"},\"top\":{\"field\":\"star_counts\"},\"width\":{\"value\":0.8},\"x\":{\"field\":\"tools\"}},\"id\":\"1350\",\"type\":\"VBar\"},{\"attributes\":{\"factors\":[\"matplotlib\",\"bokeh\",\"pyecharts\",\"seaborn\",\"plotly\",\"ggplot2\"]},\"id\":\"1318\",\"type\":\"FactorRange\"},{\"attributes\":{\"data\":{\"color\":[\"#3288bd\",\"#99d594\",\"#e6f598\",\"#fee08b\",\"#fc8d59\",\"#d53e4f\"],\"star_counts\":[9678,11034,5387,6038,4928,3783],\"tools\":[\"matplotlib\",\"bokeh\",\"pyecharts\",\"seaborn\",\"plotly\",\"ggplot2\"]},\"selected\":{\"id\":\"1360\"},\"selection_policy\":{\"id\":\"1361\"}},\"id\":\"1314\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"formatter\":{\"id\":\"1356\"},\"ticker\":{\"id\":\"1327\"}},\"id\":\"1326\",\"type\":\"CategoricalAxis\"},{\"attributes\":{\"text\":\"2019 Visualization_tools Star\"},\"id\":\"1316\",\"type\":\"Title\"},{\"attributes\":{},\"id\":\"1330\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"1360\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1361\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"data_source\":{\"id\":\"1314\"},\"glyph\":{\"id\":\"1350\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"1351\"},\"selection_glyph\":null,\"view\":{\"id\":\"1353\"}},\"id\":\"1352\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"axis\":{\"id\":\"1326\"},\"grid_line_color\":null,\"ticker\":null},\"id\":\"1328\",\"type\":\"Grid\"},{\"attributes\":{\"end\":14000,\"start\":0},\"id\":\"1320\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"1337\",\"type\":\"ResetTool\"},{\"attributes\":{},\"id\":\"1327\",\"type\":\"CategoricalTicker\"},{\"attributes\":{\"axis\":{\"id\":\"1329\"},\"dimension\":1,\"ticker\":null},\"id\":\"1332\",\"type\":\"Grid\"},{\"attributes\":{\"overlay\":{\"id\":\"1339\"}},\"id\":\"1335\",\"type\":\"BoxZoomTool\"}],\"root_ids\":[\"1315\"]},\"title\":\"Bokeh Application\",\"version\":\"2.1.1\"}};\n",
       "  var render_items = [{\"docid\":\"70559d29-81cc-4473-adb4-890da6cb7395\",\"root_ids\":[\"1315\"],\"roots\":{\"1315\":\"0e32c74d-820a-40f9-8c41-450c5b2aea07\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    var attempts = 0;\n",
       "    var timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "1315"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from bokeh.models import ColumnDataSource\n",
    "from bokeh.palettes import Spectral6\n",
    "import pandas as pd\n",
    "#数据源准备\n",
    "source=ColumnDataSource(\n",
    "    data=dict(\n",
    "        tools=df['Visualization_tools'],\n",
    "        star_counts=df['Star'],\n",
    "        color= Spectral6  #颜色也要写到source\n",
    "    \n",
    "    )\n",
    ")\n",
    "TOOLTIPS=[\n",
    "    (\"star\",\"@star_counts\"),\n",
    "    ('tools','@tools')\n",
    "]\n",
    "# 画布准备\n",
    "p = figure(\n",
    "    x_range=df['Visualization_tools'],\n",
    "    plot_height=500,\n",
    "    title=\"2019 Visualization_tools Star\",\n",
    "    tooltips=TOOLTIPS )\n",
    "# 绘制\n",
    "p.vbar(x='tools',top='star_counts',width=0.8,color='color',source=source,legend=\"tools\")\n",
    "\n",
    "\n",
    "p.xgrid.grid_line_color = None\n",
    "p.y_range.start = 0\n",
    "p.legend.location=\"top_center\"\n",
    "p.legend.orientation = \"horizontal\"\n",
    "p.y_range.end = 14000\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 分组柱状图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"c5e37e66-af72-4ea5-83e5-72ef748811a4\" data-root-id=\"1446\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  var docs_json = {\"560dd34b-e000-4a4e-be26-61083f63bd66\":{\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1456\"}],\"center\":[{\"id\":\"1458\"},{\"id\":\"1462\"}],\"left\":[{\"id\":\"1459\"}],\"plot_height\":350,\"renderers\":[{\"id\":\"1480\"}],\"title\":{\"id\":\"1447\"},\"toolbar\":{\"id\":\"1470\"},\"x_range\":{\"id\":\"1445\"},\"x_scale\":{\"id\":\"1452\"},\"y_range\":{\"id\":\"1450\"},\"y_scale\":{\"id\":\"1454\"}},\"id\":\"1446\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{},\"id\":\"1464\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"1468\",\"type\":\"HelpTool\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"1463\"},{\"id\":\"1464\"},{\"id\":\"1465\"},{\"id\":\"1466\"},{\"id\":\"1467\"},{\"id\":\"1468\"}]},\"id\":\"1470\",\"type\":\"Toolbar\"},{\"attributes\":{\"overlay\":{\"id\":\"1469\"}},\"id\":\"1465\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"1466\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"1467\",\"type\":\"ResetTool\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.9},\"x\":{\"field\":\"x\"}},\"id\":\"1479\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"1520\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1521\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"data_source\":{\"id\":\"1444\"},\"glyph\":{\"id\":\"1478\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"1479\"},\"selection_glyph\":null,\"view\":{\"id\":\"1481\"}},\"id\":\"1480\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"source\":{\"id\":\"1444\"}},\"id\":\"1481\",\"type\":\"CDSView\"},{\"attributes\":{\"data\":{\"counts\":[2,5,3,1,3,2,4,3,4,3,2,4,2,4,5,4,6,3],\"x\":[[\"Apples\",\"2015\"],[\"Apples\",\"2016\"],[\"Apples\",\"2017\"],[\"Pears\",\"2015\"],[\"Pears\",\"2016\"],[\"Pears\",\"2017\"],[\"Nectarines\",\"2015\"],[\"Nectarines\",\"2016\"],[\"Nectarines\",\"2017\"],[\"Plums\",\"2015\"],[\"Plums\",\"2016\"],[\"Plums\",\"2017\"],[\"Grapes\",\"2015\"],[\"Grapes\",\"2016\"],[\"Grapes\",\"2017\"],[\"Strawberries\",\"2015\"],[\"Strawberries\",\"2016\"],[\"Strawberries\",\"2017\"]]},\"selected\":{\"id\":\"1520\"},\"selection_policy\":{\"id\":\"1521\"}},\"id\":\"1444\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"1516\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.9},\"x\":{\"field\":\"x\"}},\"id\":\"1478\",\"type\":\"VBar\"},{\"attributes\":{\"factors\":[[\"Apples\",\"2015\"],[\"Apples\",\"2016\"],[\"Apples\",\"2017\"],[\"Pears\",\"2015\"],[\"Pears\",\"2016\"],[\"Pears\",\"2017\"],[\"Nectarines\",\"2015\"],[\"Nectarines\",\"2016\"],[\"Nectarines\",\"2017\"],[\"Plums\",\"2015\"],[\"Plums\",\"2016\"],[\"Plums\",\"2017\"],[\"Grapes\",\"2015\"],[\"Grapes\",\"2016\"],[\"Grapes\",\"2017\"],[\"Strawberries\",\"2015\"],[\"Strawberries\",\"2016\"],[\"Strawberries\",\"2017\"]],\"range_padding\":0.1},\"id\":\"1445\",\"type\":\"FactorRange\"},{\"attributes\":{\"text\":\"Fruit Counts by Year\"},\"id\":\"1447\",\"type\":\"Title\"},{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"1469\",\"type\":\"BoxAnnotation\"},{\"attributes\":{},\"id\":\"1463\",\"type\":\"PanTool\"},{\"attributes\":{\"start\":0},\"id\":\"1450\",\"type\":\"DataRange1d\"},{\"attributes\":{\"axis\":{\"id\":\"1456\"},\"grid_line_color\":null,\"ticker\":null},\"id\":\"1458\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"1452\",\"type\":\"CategoricalScale\"},{\"attributes\":{},\"id\":\"1457\",\"type\":\"CategoricalTicker\"},{\"attributes\":{},\"id\":\"1518\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"1454\",\"type\":\"LinearScale\"},{\"attributes\":{\"formatter\":{\"id\":\"1516\"},\"major_label_orientation\":1,\"ticker\":{\"id\":\"1457\"}},\"id\":\"1456\",\"type\":\"CategoricalAxis\"},{\"attributes\":{\"formatter\":{\"id\":\"1518\"},\"ticker\":{\"id\":\"1460\"}},\"id\":\"1459\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1460\",\"type\":\"BasicTicker\"},{\"attributes\":{\"axis\":{\"id\":\"1459\"},\"dimension\":1,\"ticker\":null},\"id\":\"1462\",\"type\":\"Grid\"}],\"root_ids\":[\"1446\"]},\"title\":\"Bokeh Application\",\"version\":\"2.1.1\"}};\n",
       "  var render_items = [{\"docid\":\"560dd34b-e000-4a4e-be26-61083f63bd66\",\"root_ids\":[\"1446\"],\"roots\":{\"1446\":\"c5e37e66-af72-4ea5-83e5-72ef748811a4\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    var attempts = 0;\n",
       "    var timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "1446"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "## 构建一对多的关系\n",
    "from bokeh.models import ColumnDataSource, FactorRange\n",
    "fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']\n",
    "years = ['2015', '2016', '2017']\n",
    "data = {'fruits' : fruits,\n",
    "        '2015'   : [2, 1, 4, 3, 2, 4],\n",
    "        '2016'   : [5, 3, 3, 2, 4, 6],\n",
    "        '2017'   : [3, 2, 4, 4, 5, 3]}\n",
    "\n",
    "x = [ (fruit, year) for fruit in fruits for year in years ]\n",
    "counts = sum(zip(data['2015'], data['2016'], data['2017']), ()) # 分组求和(堆叠总数)\n",
    "source = ColumnDataSource(data=dict(x=x, counts=counts))\n",
    "# 画布\n",
    "p = figure(x_range=FactorRange(*x), plot_height=350, title=\"Fruit Counts by Year\",\n",
    "#            toolbar_location=None, tools=\"\"\n",
    "          )\n",
    "# 柱状图\n",
    "p.vbar(x='x', top='counts', width=0.9, source=source)\n",
    "# 其他\n",
    "p.y_range.start = 0\n",
    "p.x_range.range_padding = 0.1\n",
    "p.xaxis.major_label_orientation = 1\n",
    "p.xgrid.grid_line_color = None\n",
    "# 显示\n",
    "show(p)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 课后实践（star，commits，contributes）分组柱状图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bokeh.models import HoverTool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"af3a03a4-47c9-4da3-95e6-0561801cd05c\" data-root-id=\"3118\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  var docs_json = {\"34241e08-4233-48eb-8800-966348eb79b7\":{\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"3128\"}],\"center\":[{\"id\":\"3130\"},{\"id\":\"3134\"}],\"left\":[{\"id\":\"3131\"}],\"plot_height\":350,\"renderers\":[{\"id\":\"3152\"}],\"title\":{\"id\":\"3119\"},\"toolbar\":{\"id\":\"3142\"},\"toolbar_location\":null,\"x_range\":{\"id\":\"3117\"},\"x_scale\":{\"id\":\"3124\"},\"y_range\":{\"id\":\"3122\"},\"y_scale\":{\"id\":\"3126\"}},\"id\":\"3118\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{},\"id\":\"3136\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":1},\"x\":{\"field\":\"x\"}},\"id\":\"3150\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"3140\",\"type\":\"HelpTool\"},{\"attributes\":{\"overlay\":{\"id\":\"3141\"}},\"id\":\"3137\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"3138\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"3139\",\"type\":\"ResetTool\"},{\"attributes\":{\"axis\":{\"id\":\"3128\"},\"grid_line_color\":null,\"ticker\":null},\"id\":\"3130\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"3269\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{},\"id\":\"3124\",\"type\":\"CategoricalScale\"},{\"attributes\":{\"formatter\":{\"id\":\"3269\"},\"major_label_orientation\":1,\"ticker\":{\"id\":\"3129\"}},\"id\":\"3128\",\"type\":\"CategoricalAxis\"},{\"attributes\":{\"text\":\"Fruit Counts by Year\"},\"id\":\"3119\",\"type\":\"Title\"},{\"attributes\":{\"formatter\":{\"id\":\"3271\"},\"ticker\":{\"id\":\"3132\"}},\"id\":\"3131\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"3132\",\"type\":\"BasicTicker\"},{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"3141\",\"type\":\"BoxAnnotation\"},{\"attributes\":{},\"id\":\"3271\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"axis\":{\"id\":\"3131\"},\"dimension\":1,\"ticker\":null},\"id\":\"3134\",\"type\":\"Grid\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":1},\"x\":{\"field\":\"x\"}},\"id\":\"3151\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"3129\",\"type\":\"CategoricalTicker\"},{\"attributes\":{\"source\":{\"id\":\"3115\"}},\"id\":\"3153\",\"type\":\"CDSView\"},{\"attributes\":{\"start\":0},\"id\":\"3122\",\"type\":\"DataRange1d\"},{\"attributes\":{\"data_source\":{\"id\":\"3115\"},\"glyph\":{\"id\":\"3150\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"3151\"},\"selection_glyph\":null,\"view\":{\"id\":\"3153\"}},\"id\":\"3152\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"factors\":[[\"matplotlib\",\"star\"],[\"matplotlib\",\"conmmits\"],[\"matplotlib\",\"contributors\"],[\"bokeh\",\"star\"],[\"bokeh\",\"conmmits\"],[\"bokeh\",\"contributors\"],[\"pyecharts\",\"star\"],[\"pyecharts\",\"conmmits\"],[\"pyecharts\",\"contributors\"],[\"seaborn\",\"star\"],[\"seaborn\",\"conmmits\"],[\"seaborn\",\"contributors\"],[\"plotly\",\"star\"],[\"plotly\",\"conmmits\"],[\"plotly\",\"contributors\"],[\"ggplot2\",\"star\"],[\"ggplot2\",\"conmmits\"],[\"ggplot2\",\"contributors\"]],\"range_padding\":0.1},\"id\":\"3117\",\"type\":\"FactorRange\"},{\"attributes\":{\"callback\":null,\"tooltips\":[[\"star\",\"@star\"],[\"conmmits\",\"@conmmits\"],[\"contributors\",\"@contributors\"]]},\"id\":\"3116\",\"type\":\"HoverTool\"},{\"attributes\":{\"data\":{\"counts\":[9678,29503,808,11034,17673,357,5387,1321,18,6038,2316,98,4928,3370,76,3783,4286,184],\"x\":[[\"matplotlib\",\"star\"],[\"matplotlib\",\"conmmits\"],[\"matplotlib\",\"contributors\"],[\"bokeh\",\"star\"],[\"bokeh\",\"conmmits\"],[\"bokeh\",\"contributors\"],[\"pyecharts\",\"star\"],[\"pyecharts\",\"conmmits\"],[\"pyecharts\",\"contributors\"],[\"seaborn\",\"star\"],[\"seaborn\",\"conmmits\"],[\"seaborn\",\"contributors\"],[\"plotly\",\"star\"],[\"plotly\",\"conmmits\"],[\"plotly\",\"contributors\"],[\"ggplot2\",\"star\"],[\"ggplot2\",\"conmmits\"],[\"ggplot2\",\"contributors\"]]},\"selected\":{\"id\":\"3273\"},\"selection_policy\":{\"id\":\"3274\"}},\"id\":\"3115\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"3135\"},{\"id\":\"3136\"},{\"id\":\"3137\"},{\"id\":\"3138\"},{\"id\":\"3139\"},{\"id\":\"3140\"},{\"id\":\"3116\"}]},\"id\":\"3142\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"3126\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"3273\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"3135\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"3274\",\"type\":\"UnionRenderers\"}],\"root_ids\":[\"3118\"]},\"title\":\"Bokeh Application\",\"version\":\"2.1.1\"}};\n",
       "  var render_items = [{\"docid\":\"34241e08-4233-48eb-8800-966348eb79b7\",\"root_ids\":[\"3118\"],\"roots\":{\"3118\":\"af3a03a4-47c9-4da3-95e6-0561801cd05c\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    var attempts = 0;\n",
       "    var timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "3118"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "## 构建一对多的关系\n",
    "from bokeh.models import ColumnDataSource, FactorRange\n",
    "fruits = df['Visualization_tools']\n",
    "groups = ['star', 'conmmits', 'contributors']\n",
    "data = {'fruits' : fruits,\n",
    "        'star'   : df['Star'],\n",
    "        'conmmits'   : df['Commits'],\n",
    "        'contributors'   : df['Contributors']}\n",
    "\n",
    "x = [ (fruit, group) for fruit in fruits for group in groups ]\n",
    "counts = sum(zip(data['star'], data['conmmits'], data['contributors']), ()) # 分组求和(堆叠总数)\n",
    "source = ColumnDataSource(data=dict(x=x, counts=counts))\n",
    "hover1 = HoverTool(tooltips=[\n",
    "    (\"star\",\"@star\"),\n",
    "    (\"conmmits\",\"@conmmits\"),\n",
    "    (\"contributors\",\"@contributors\")\n",
    "])\n",
    "# 画布\n",
    "p = figure(\n",
    "    x_range=FactorRange(*x),\n",
    "    plot_height=350, \n",
    "    title=\"Fruit Counts by Year\",\n",
    "    toolbar_location=None\n",
    ")\n",
    "# 柱状图\n",
    "p.vbar(x='x', top='counts', width=1,source=source)\n",
    "# 其他\n",
    "p.y_range.start = 0\n",
    "p.x_range.range_padding = 0.1\n",
    "p.xaxis.major_label_orientation = 1\n",
    "p.xgrid.grid_line_color = None\n",
    "p.add_tools(hover1)\n",
    "# 显示\n",
    "show(p)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
